// import { TeaDefine } from "../../../kds-base-define/src/TeaDefine"
// import { ServerValues } from "../../../kds-base-define/src/ServerConfig"
// import { DB } from "../db"
// import { Log } from "../log"
// import { RpcRoomTea } from "./room"
// import { Rpc } from "../rpc"
// import { MutexDefine } from "../../../kds-base-define/src/MutexDefine"
// import { RpcUserInfo, RpcUserLogin } from "./user"
// import { kdutils } from "kdweb-core/lib/utils"


// let dataTablename = "t_tea_data"
// let authTablename = "t_tea_auth"
// let memberTablename = "t_tea_member"
// let templateTablename = "t_tea_template"
// let accountTablename = " t_tea_account"

// let db = DB.get(ServerValues.dbTea,"tea-get")

// async function getTeaData(h:string,codeOrID:string | number) {
// 	let teaData:TeaDefine.TeaData = null
// 	if(typeof(codeOrID) === "string") {
// 		teaData = await db.getSingle(dataTablename,{teaCode:codeOrID})
// 	} else {
// 		teaData = await db.getSingle(dataTablename,{teaID:codeOrID})
// 	}
// 	return teaData
// }

// async function getTeaAuth(h:string,teaID:number) {
// 	let teaAuth:TeaDefine.TeaAuth = await db.getSingle(authTablename,{teaID:teaID})
// 	return teaAuth
// }

// async function getTeaMember(h:string,teaID:number) {
// 	let teaMember:TeaDefine.TeaMember = await db.getSingle(memberTablename,{teaID:teaID})
// 	return teaMember
// }

// async function getTeaTemplate(h:string,teaID:number) {
// 	let teaTemplate:TeaDefine.TeaTemplate = await db.getSingle(templateTablename,{teaID:teaID})
// 	return teaTemplate
// }

// async function getTeaRoom(h:string,teaID:number) {
// 	let rooms = await RpcRoomTea.get(h,teaID)
// 	let ret:TeaDefine.TeaRoom = {
// 		teaID:teaID,
// 		rooms:rooms
// 	}
// 	return ret
// }

// async function getTeaAccount(h:string,teaID:number) {
// 	let teaAccount:TeaDefine.TeaAccount = await db.getSingle(accountTablename,{teaID:teaID})
// 	return teaAccount
// }

// async function getTeaFullData(h:string,teaID:number) {
// 	let teaData = await getTeaData(h,teaID)
// 	if(teaData == null) {
// 		Log.oth.error("[tea-get] getTeaFullData: getTeaData failed teaID = " + teaID)
// 		return null
// 	}
// 	let ret:TeaDefine.TeaFullData = {
// 		teaID:teaData.teaID,
// 		teaCode:teaData.teaCode,
		
// 		data:teaData,
// 		auth:null,
// 		member:null,
// 		template:null,
// 		account:null,
// 		room:null,
// 	}
// 	ret.auth = await getTeaAuth(h,teaID)
// 	if(ret.auth == null) {
// 		Log.oth.error("[tea-get] getTeaFullData: getTeaData failed teaID = " + teaID)
// 		return null
// 	}
// 	ret.member = await getTeaMember(h,teaID)
// 	if(ret.member == null) {
// 		Log.oth.error("[tea-get] getTeaFullData: getTeaMember failed teaID = " + teaID)
// 		return null
// 	}
// 	ret.template = await getTeaTemplate(h,teaID)
// 	if(ret.template == null) {
// 		Log.oth.error("[tea-get] getTeaFullData: getTeaTemplate failed teaID = " + teaID)
// 		return null
// 	}
// 	ret.account = await getTeaAccount(h,teaID)
// 	if(ret.account == null) {
// 		Log.oth.error("[tea-get] getTeaFullData: getTeaAccount failed teaID = " + teaID)
// 		return null
// 	}
// 	ret.room = await getTeaRoom(h,teaID)
// 	if(ret.room == null) {
// 		Log.oth.error("[tea-get] getTeaFullData: getTeaRoom failed teaID = " + teaID)
// 		return null
// 	}
// 	return ret 
// }

// async function getTeaIDbyCode(h:string,teaCode:string) {
// 	let teaData:TeaDefine.TeaData = await db.getSingle(dataTablename,{teaCode:teaCode})
// 	if(teaData == null) {
// 		return null
// 	}
// 	return teaData.teaID
// }

// async function getTeaCodeByID(h:string,teaID:number) {
// 	let teaData:TeaDefine.TeaData = await db.getSingle(dataTablename,{teaID:teaID})
// 	if(teaData == null) {
// 		return null
// 	}
// 	return teaData.teaCode
// }

// let dbTeaCreate = DB.get(ServerValues.dbTea,"tea-create")
// async function createTea(h:string,bossUserID:number,teaName?:string) {
// 	let loginData = await RpcUserLogin.get(h,bossUserID)
// 	if(loginData == null) {
// 		return null 
// 	}
// 	let teaID = await Rpc.center.callException("kds.ids.getId","teaID",800000,1)
// 	if(teaID == null) {
// 		return null
// 	}
// 	let teaCode = await Rpc.center.callException("kds.code.get",6,"teaCode") 
// 	if(teaCode == null) {
// 		return null
// 	}
	
// 	let teaFullData:TeaDefine.TeaFullData = {
// 		teaID:teaID,
// 		teaCode:teaCode,
// 		data: {
// 			teaID:teaID,
// 			teaCode:teaCode,

// 			teaMode:TeaDefine.TeaModeType.Normal,

// 			teaName:teaName || loginData.nickName,
			
// 			bossUserID:loginData.userID,
// 			bossSK:loginData.sk,

// 			regDate:kdutils.getFmtMoment("YYYY-MM-DD HH:mm:ss"),
// 			regTimestamp:kdutils.getMillionSecond(),

// 			cardCount:0,

// 			status:TeaDefine.TeaStatus.Normal,
// 		},
// 		auth: {
// 			teaID:teaID,
// 			autoDesk:false,
// 			allowGuest:true,
// 			adminAddMember:true,
// 			adminRemoveMember:true,

// 			adminOperRoom:true,
// 			adminOperTemplate:true,
// 		},
// 		member: {
// 			teaID:teaID,
// 			members:[],
// 			admins:[],
// 			proxys:[],
// 			groups:[],
// 			leaderRelations:[],
// 			reqMemberIDs:[],
// 			inviteMemberIDs:[],
// 		},
// 		template: {
// 			teaID:teaID,
// 			items:[],
// 		},
// 		account: {
// 			teaID:teaID,
// 			users:[],
// 		},
// 		room:null
// 	}

// 	let b = true 
// 	do {
// 		b = b && await dbTeaCreate.insert(dataTablename,teaFullData.data) > 0
// 		if(!b) break;
// 		b = b && await dbTeaCreate.insert(authTablename,teaFullData.auth) > 0
// 		if(!b) break;
// 		b = b && await dbTeaCreate.insert(memberTablename,teaFullData.member) > 0
// 		if(!b) break;
// 		b = b && await dbTeaCreate.insert(templateTablename,teaFullData.template) > 0
// 		if(!b) break;
// 		b = b && await dbTeaCreate.insert(accountTablename,teaFullData.account) > 0
// 		if(!b) break;
// 	} while(false)
// 	if(!b) {
// 		Log.oth.error("create tea failed when insert userID = " + bossUserID + " teaName = " + teaName)
// 		dbTeaCreate.del(dataTablename,{teaID:teaID})
// 		dbTeaCreate.del(authTablename,{teaID:teaID})
// 		dbTeaCreate.del(memberTablename,{teaID:teaID})
// 		dbTeaCreate.del(templateTablename,{teaID:teaID})
// 		dbTeaCreate.del(accountTablename,{teaID:teaID})
// 		Rpc.center.callException("kds.code.release",teaCode) 
// 		return null
// 	}
// 	return teaFullData
// }

// async function changeCode(h:string,teaID:number,newTeaCode:string) {
// 	let teaData:TeaDefine.TeaData = await db.getSingle(dataTablename,{teaID:teaID})
// 	if(teaData == null) {
// 		return null
// 	}
// 	if(await db.getSingle(dataTablename,{teaCode:newTeaCode}) != null) {
// 		return null 
// 	}
// 	teaData.teaCode = newTeaCode
// 	let b = await updateTeaData(h,teaData)
// 	if(!b) {
// 		return null
// 	}
// 	return teaData
// }

// export let RpcTea = {
// 	id:getTeaIDbyCode,
// 	code:getTeaCodeByID,

// 	create:createTea,
// 	changeCode:changeCode
// }

// export let RpcTeaGet = {
// 	data:getTeaData,
// 	auth:getTeaAuth,
// 	member:getTeaMember,
// 	template:getTeaTemplate,
// 	account:getTeaAccount,
// 	room:getTeaRoom,

// 	full:getTeaFullData,
// }


// let dbTeaUpdate = DB.get(ServerValues.dbTea,"tea-update")
// async function updateTeaData(h:string,teaData:TeaDefine.TeaData) {
// 	return await dbTeaUpdate.update(dataTablename,{teaID:teaData.teaID},teaData) > 0
// }

// async function updateTeaAuth(h:string,teaAuth:TeaDefine.TeaAuth) {
// 	return await dbTeaUpdate.update(authTablename,{teaID:teaAuth.teaID},teaAuth) > 0
// }

// async function updateTeaMember(h:string,teaMember:TeaDefine.TeaMember) {
// 	return await dbTeaUpdate.update(memberTablename,{teaID:teaMember.teaID},teaMember) > 0
// }

// async function updateTeaTemplate(h:string,teaTemplate:TeaDefine.TeaTemplate) {
// 	return await dbTeaUpdate.update(templateTablename,{teaID:teaTemplate.teaID},teaTemplate) > 0
// }

// async function updateTeaAccount(h:string,teaAccount:TeaDefine.TeaAccount) {
// 	return await dbTeaUpdate.update(accountTablename,{teaID:teaAccount.teaID},teaAccount) > 0
// }

// export let RpcTeaUpdate = {
// 	data:updateTeaData,
// 	auth:updateTeaAuth,
// 	member:updateTeaMember,
// 	template:updateTeaTemplate,
// 	account:updateTeaAccount,
// }
